import { Album, AlbumSearchParams } from '@/api/photoAlbum/model';
import request from '@/utils/request';
import { ApiResult, PageResult } from '@/api';

/**
 * 添加相册
 * @param data
 */
export async function addAlbum(data: Album) {
  const res = await request.post<ApiResult<unknown>>('/album', data);
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 更新相册
 * @param data
 */
export async function updateAlbum(data: Album) {
  const res = await request.put<ApiResult<unknown>>('/album', data);
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取相册列表
 * @param data
 */
export async function getAlbumList(data: AlbumSearchParams) {
  const res = await request.get<ApiResult<PageResult<Album>>>('/album', {
    params: data
  });
  if (res.data.code === 0) {
    const pageData = {
      list: res.data.data?.records,
      count: res.data.data?.total
    };
    return pageData;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改相册状态
 * @param id
 * @param status
 */
export async function updateAlbumStatus(
  id?: string,
  name?: string,
  cover?: string,
  status?: boolean
) {
  const res = await request.put<ApiResult<unknown>>('/album', {
    id: id,
    name: name,
    cover: cover,
    status: status
  });
  if (res.data.code === 0) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
